package org.ggumm21.moneybook;

import java.util.Calendar;
import java.util.Date;
import java.util.logging.Logger;

import javax.persistence.PrePersist;
import javax.persistence.PreRemove;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class AbstractDataRecord {

	Date createdDate;
	Date lastUpdatedDate;
	User creator;
	User lastUpdater;

	private static final Logger log = Logger.getLogger(AbstractDataRecord.class
			.getName());
	
	@PrePersist void fillCommonFields() {
        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser(); 
        if (createdDate == null) {
			createdDate = Calendar.getInstance().getTime();
			if (user != null)
				creator = user;
		} 
		lastUpdatedDate = Calendar.getInstance().getTime();
		if (user != null)
			lastUpdater = user;
		log.info("PREPERSIST : " + this.toString());
	}
	
	@PreRemove void checkBeforeDelete() {
		System.out.println("");
	}
	
	public Date getCreatedDate() {
		return createdDate;
	}
	
	public Date getLastUpdatedDate() {
		return lastUpdatedDate;
	}
	
	public String getCreator() {
		return (creator==null?"":creator.getNickname());
	}
	
	public String getLastUpdater() {
		return (lastUpdater==null?"":lastUpdater.getNickname());
	}
	
}
